package com.github.prontera.service;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.time.OffsetDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.domain.Client;
import com.github.prontera.domain.ClientSign;
import com.github.prontera.domain.SysDict;
import com.github.prontera.domain.Task;
import com.github.prontera.domain.enums.ConsumeLevelTypeEnum;
import com.github.prontera.domain.enums.ConsumeSmallTypeEnum;
import com.github.prontera.domain.enums.ConsumeTypeEnum;
import com.github.prontera.domain.enums.DictTypeEnum;
import com.github.prontera.domain.enums.TaskTypeEnum;
import com.github.prontera.model.ConsumeMessageTemplate;
import com.github.prontera.persistence.ClientSignMapper;
import com.github.prontera.persistence.CrudMapper;
import com.github.prontera.util.DateTimeUtil;

/**
 * 客户端签到
 */
@Service
public class ClientSignService extends CrudServiceImpl<ClientSign>{
    
	@Autowired
	private ClientService clientService;
	@Autowired
	private ConsumeService consumeService;
	@Autowired
	private ClientSignMapper clientSignMapper;
	@Autowired
	private TaskService taskService;
	
  	@Autowired
    public ClientSignService(CrudMapper<ClientSign> mapper) {
        super(mapper);
    }

  	public Long daySignCount(){
		return clientSignMapper.daySignCount();
  	}
  	
  	public Integer calSignAmount(Long clientId,BigDecimal baseAmount) {
  		if(baseAmount==null||baseAmount.compareTo(new BigDecimal(0))<=0){
  			return 0;
  		}
		List<SysDict> sysDicts=CommonDict.get(DictTypeEnum.SIGN_DAY.getDesc());
		ClientSign clientSign=new ClientSign();
		clientSign.setBaseAmount(baseAmount);
		clientSign.setClientId(clientId);
		
		SysDict preDict=null;
		for (int i = 0; i < sysDicts.size(); i++) {
			SysDict sysDict=sysDicts.get(i);
			BigDecimal amount= new BigDecimal(sysDict.getLable());
			BigDecimal preAmount=null;
			if(preDict!=null){
				preAmount= new BigDecimal(preDict.getLable());
			}
			if(preDict==null && baseAmount.compareTo(amount)<=0){
				clientSign.setMaxRateAmount(new BigDecimal(sysDict.getLable()));
				clientSign.setRateInterval(sysDict.getValue().doubleValue());
				clientSign.setIncomeBean(baseAmount.multiply(sysDict.getValue()).divide(new BigDecimal("100")).setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
				clientSign.setMaxRateIntervalId(sysDict.getId());
				break;
			}else if (preAmount!=null&& baseAmount.compareTo(preAmount)>0 && baseAmount.compareTo(new BigDecimal(sysDict.getLable()))<=0){
				clientSign.setMinRateAmount(preAmount);
				clientSign.setMaxRateAmount(amount);
				clientSign.setRateInterval(sysDict.getValue().doubleValue());
				clientSign.setIncomeBean(baseAmount.multiply(sysDict.getValue()).divide(new BigDecimal("100")).setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
				clientSign.setMinRateIntervalId(preDict.getId());
				clientSign.setMaxRateIntervalId(sysDict.getId());
				break;
			}
			preDict=sysDicts.get(i);
		}
		
		if(clientSign.getRateInterval()==null){
			System.out.println("-------------未找到合适得区间数据---------------");
			return 0;
		}
		return clientSign.getIncomeBean();
	}
  	
  	public Integer sign(Long clientId) {
  		Client client=clientService.get(clientId);
		if(client.getLastSignTime()!=null&&client.getLastSignTime().getYear()==OffsetDateTime.now().getYear()&&client.getLastSignTime().getDayOfYear()==OffsetDateTime.now().getDayOfYear()){
			//已经签到
			return 0;
		}
		BigDecimal baseAmount=client.getAmount().add(client.getDepositAmount()).add(client.getCashAmount());
		List<SysDict> sysDicts=CommonDict.get(DictTypeEnum.SIGN_DAY.getDesc());
		
		ClientSign clientSign=new ClientSign();
		clientSign.setBaseAmount(baseAmount);
		clientSign.setClientId(clientId);
		
		SysDict preDict=null;
		for (int i = 0; i < sysDicts.size(); i++) {
			SysDict sysDict=sysDicts.get(i);
			BigDecimal amount= new BigDecimal(sysDict.getLable());
			BigDecimal preAmount=null;
			if(preDict!=null){
				preAmount= new BigDecimal(preDict.getLable());
			}
			if(preDict==null && baseAmount.compareTo(amount)<=0){
				clientSign.setMaxRateAmount(new BigDecimal(sysDict.getLable()));
				clientSign.setRateInterval(sysDict.getValue().doubleValue());
				clientSign.setIncomeBean(baseAmount.multiply(sysDict.getValue()).divide(new BigDecimal("100")).setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
				clientSign.setMaxRateIntervalId(sysDict.getId());
				break;
			}else if (preAmount!=null&& baseAmount.compareTo(preAmount)>0 && baseAmount.compareTo(new BigDecimal(sysDict.getLable()))<=0){
				clientSign.setMinRateAmount(preAmount);
				clientSign.setMaxRateAmount(amount);
				clientSign.setRateInterval(sysDict.getValue().doubleValue());
				clientSign.setIncomeBean(baseAmount.multiply(sysDict.getValue()).divide(new BigDecimal("100")).setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
				clientSign.setMinRateIntervalId(preDict.getId());
				clientSign.setMaxRateIntervalId(sysDict.getId());
				break;
			}
			preDict=sysDicts.get(i);
		}
		
		if(clientSign.getRateInterval()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "未找到合适得区间数据");
		}
		
		if(clientSignMapper.preDaySign(clientId)==0){//不是连续签到
			Client updateClient=new Client();
			updateClient.setId(clientId);
			updateClient.setSignCount(1);
			updateClient.setLastSignTime(OffsetDateTime.now());
			clientService.update(updateClient);
			
			//系统取消任务
			taskService.systemCancelTask(clientId, TaskTypeEnum.ONE);
		}else{
			Client updateClient=new Client();
			updateClient.setId(clientId);
			updateClient.setSignCount(1);
			updateClient.setLastSignTime(OffsetDateTime.now());
			clientService.updateCount(updateClient);
			taskService.pushTaskResult(TaskTypeEnum.ONE,clientId,false);
		}
		if(clientService.updateMyBean(clientId, clientSign.getIncomeBean())){
			String lable=MessageFormat.format(ConsumeMessageTemplate.MSG_SIGN_DAY_BEAN,DateTimeUtil.getSystemDate(), clientSign.getIncomeBean());
			consumeService.insertConsume(clientId,ConsumeSmallTypeEnum.SIGN_DAY,lable ,ConsumeLevelTypeEnum.BEAN,  clientSign.getIncomeBean(), null,ConsumeTypeEnum.INCOME,null);
		}
		clientSign.setSignDate(OffsetDateTime.now());
  		
  		insert(clientSign);
  		
		return clientSign.getIncomeBean();
	}

	public List<ClientSign> find2(ClientSign clientSign) {
		return clientSignMapper.find2(clientSign);
	}
  	
}
